<?php
/**
 * @Notes:【】
 * @User: zwc
 * @Date: 2022-05-01 17:20
 */

namespace App\Dao\Chat;

use App\Model\Chat\ChatFd;
use Yj\Daos\BaseDao;
use Hyperf\Database\Model\Builder;
use Yj\Daos\Verify;

/**
 * @var \App\Model\Chat\ChatRecords
 */
class ChatRecordsDao extends BaseDao
{
    public function DaoWhere(array $params)
    {
        return $this->getDaoQuery($params, function (Verify $verify) {
            $verify('id', function (Builder $query, $id) {
                $query->where('id', $id);
            });
            $verify('user_id', function (Builder $query, $value) {
                $query->where('user_id', $value);
            });
            $verify('receiver_id', function (Builder $query, $value) {
                $query->where('receiver_id', $value);
            });
        });
    }

    public function getRecords($userId, $receiverId)
    {
        return $this->where(function (Builder $query) use ($userId, $receiverId) {
            $query->orWhere(function ($query) use ($userId, $receiverId) {
                $query->where('user_id', $userId)->where('receiver_id', $receiverId);
            });
            $query->orWhere(function ($query) use ($userId, $receiverId) {
                $query->where('user_id', $receiverId)->where('receiver_id', $userId);
            });
        })->with(['ChatMessage', 'Receiver', 'User'])->orderByDesc('id')->getList();
    }

    public function getUnReadNum($userId)
    {
        return $this->DaoWhere([
            'receiver_id' => $userId
        ])->where('is_read', 0)->count();
    }
    
}
